//
//  CZEmotionKeyBoardView.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZEmotionKeyBoardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 设置UI
    func setupUI(){
        
         backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        //添加控件
        addSubview(emoticonBottomView)
        addSubview(emotionPageView)
        addSubview(pageControl)
        
        
        //添加约束
        emoticonBottomView.snp_makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
     
        emotionPageView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(emoticonBottomView.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) in
            make.bottom.equalTo(emotionPageView).offset(5)
            make.centerX.equalTo(emotionPageView)
        }
        
        //MARK: - 底部视图闭包回调
        emoticonBottomView.closure = {(type:CZEmotionBottomViewType)->() in
  
            //获取坐标
            let indexpath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            //设置滚动到的位置
            self.emotionPageView.scrollToItemAtIndexPath(indexpath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
           self.setupPageControl(indexpath)
            
        }
        
        
        //刚一进来就显示
        setupPageControl(NSIndexPath(forItem: 0, inSection: 0))

}
    
    
    //MARK: - 懒加载控件
    private lazy var emoticonBottomView:CZEmotionBottomView = CZEmotionBottomView(frame:CGRectZero)
    
    //表情键盘的加载
    private lazy var emotionPageView:CZEmotionPageView = {
        
        let pageView = CZEmotionPageView()
        //设置分页效果
        pageView.pagingEnabled = true
        //颜色
        pageView.backgroundColor = self.backgroundColor
        //取消弹簧效果
        pageView.bounces = false
        //设置代理
        pageView.delegate = self
        //隐藏滚动条
        pageView.showsVerticalScrollIndicator = false
        pageView.showsHorizontalScrollIndicator = false
        return pageView
    }()
    
    //pageControl的加载
    
    private lazy var pageControl:UIPageControl = {
       
        let view = UIPageControl()

        // 只用kvc 给只读属性赋值 (xcode 6.4 以后 就不在开放了 但是可以使用 前提 你的知道他
        view.setValue(UIImage(named:"compose_keyboard_dot_normal"), forKey: "pageImage")
        view.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        //取消交互
        view.userInteractionEnabled = false
        
        return view
    }()
    
    
}

extension CZEmotionKeyBoardView:UICollectionViewDelegate{
    
    //实现代理方法
    func scrollViewDidScroll(scrollView: UIScrollView) {
         // 获取屏幕中心点x (相对于pageView)
        let centerX = SCREENW*0.5 + scrollView.contentOffset.x
        
        //获取屏幕显示的cells (由于系统没有帮我们排序 需要我们自己排序)
        let cells = self.emotionPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        //屏幕有俩个cell的时候再进行判断
        if cells.count == 2 {
            //第一个cell
            let firstCell = cells.first!
            //第二个cell
            let lastCell = cells.last!
            
            //定义一个indexpath
            let indexpath:NSIndexPath
            
            //判断中心点
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
//                print("第一个cell多")
                //获取当前cell的indexpath
                indexpath = emotionPageView.indexPathForCell(firstCell)!
                
            }else{
//                print("第二个cell多")
                indexpath = emotionPageView.indexPathForCell(lastCell)!
            }
            //赋值
            emoticonBottomView.setupCurrentButton(indexpath.section)
            setupPageControl(indexpath)
        }
        
    }
    
    
    //设置pageControl的页数
    func setupPageControl(indexpath:NSIndexPath){
        
        //总页数
        pageControl.numberOfPages = CZEmoticonTools.shareTools.allEmoticons[indexpath.section].count
        //当前页数
        pageControl.currentPage = indexpath.item
    }
    
    
}







